home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snpd0492.zip / SETENVAR.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  4KB  |  152 lines

  1. /*
  2. **  SETENVAR.C - Program which sets the DOS master environment upon exit
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <conio.h>
  16. #include <dos.h>
  17.  
  18. #if !defined(__ZTC__) && !defined(__TURBOC__)
  19.  #define MK_FP(seg,offset) \
  20.         ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  21.  #define peek(s,o) (*((unsigned far *)(MK_FP(s,o))))
  22.  #define poke(s,o,w) (*((unsigned far *)(MK_FP(s,o)))=(w))
  23. #endif
  24.  
  25. #define SUCCESS 0
  26. #define ERROR -1
  27.  
  28. static unsigned head, tail;
  29. static int idx = 0;
  30. static unsigned keystack[16][2];
  31.  
  32. /*
  33. **  ungetkey()
  34. **
  35. **  Stuffs characters into the keyboard buffer.
  36. **
  37. **  Parameters: 1 - Extended character to stuff
  38. **
  39. **  Returns: SUCCESS or EOF
  40. **
  41. **  Note: This function assumes that the keyboard buffer is in
  42. **        the normal (for IBM) location of 40:1E.
  43. **
  44. */
  45.  
  46. int ungetkey(unsigned key)
  47. {
  48.         int count;
  49.  
  50. #ifdef __ZTC__
  51.         peek(0x40, 0x1a, &head, sizeof(unsigned));
  52.         peek(0x40, 0x1c, &tail, sizeof(unsigned));
  53. #else
  54.         head = peek(0x40, 0x1a);
  55.         tail = peek(0x40, 0x1c);
  56. #endif
  57.         count = tail - head;
  58.         if (0 > count)
  59.                 count += (16 * sizeof(unsigned));
  60.         count >>= 1;
  61.  
  62.         if (15 > count)
  63.         {
  64. #ifdef __ZTC__
  65.                 peek(0x40, tail, &keystack[idx][0], sizeof(unsigned));
  66. #else
  67.                 keystack[idx][0] = peek(0x40, tail);
  68. #endif
  69.                 keystack[idx][1] = tail;
  70. #ifdef __ZTC__
  71.                 poke(0x40, tail, &key, sizeof(unsigned));
  72. #else
  73.                 poke(0x40, tail, key);
  74. #endif
  75.                 tail += sizeof(unsigned);
  76.                 if (0x3e <= tail)
  77.                         tail = 0x1e;
  78. #ifdef __ZTC__
  79.                 poke(0x40, 0x1c, &tail, sizeof(unsigned));
  80. #else
  81.                 poke(0x40, 0x1c, tail);
  82. #endif
  83.                 return key;
  84.         }
  85.         return EOF;
  86. }
  87.  
  88. /*
  89. **  KB_stuff()
  90. **
  91. **  Stuffs strings into the keyboard buffer.
  92. **
  93. **  Parameters: 1 - String to stuff
  94. **
  95. **  Returns: SUCCESS if successful
  96. **           ERROR   in case of error, plus beyboard buffer is
  97. **                   restored
  98. **
  99. **  Note: This function assumes that the keyboard buffer is in
  100. **        the normal (for IBM) location of 40:1E.
  101. */
  102.  
  103. int KB_stuff(char *str)
  104. {
  105.         int ercode = SUCCESS;
  106.  
  107.         idx = 0;
  108.         while (*str)
  109.         {
  110.                 if (EOF == ungetkey((unsigned)(*str++)))
  111.                 {
  112.                         while (0 <= --idx)
  113.                         {
  114.                                 tail = keystack[idx][1];
  115. #ifdef __ZTC__
  116.                                 poke(0x40, tail, &keystack[idx][0],
  117.                                         sizeof(unsigned));
  118. #else
  119.                                 poke(0x40, tail, keystack[idx][0]);
  120. #endif
  121.                         }
  122. #ifdef __ZTC__
  123.                         poke(0x40, 0x1c, &tail, sizeof(unsigned));
  124. #else
  125.                         poke(0x40, 0x1c, tail);
  126. #endif
  127.                         ercode = ERROR;
  128.                         break;
  129.                 }
  130.                 else    ++idx;
  131.         }
  132.         idx = 0;
  133.         return ercode;
  134. }
  135.  
  136. void main(int argc, char *argv[])
  137. {
  138.         FILE *bfile;
  139.  
  140.         if (3 > argc)
  141.         {
  142.                 puts("\aUsage: SETENVAR envar datum");
  143.                 abort();
  144.         }
  145.         bfile = fopen("$TMP$.BAT", "w");
  146.         fprintf(bfile, "SET %s=%s\ndel $tmp$.bat\x1a", argv[1], argv[2]);
  147.         fclose(bfile);
  148.         while (kbhit())
  149.                 getch();
  150.         KB_stuff("$tmp$\r");
  151. }
  152.